// UsesStatTable.cpp : stores the variables used in each statement.
// Author: Achyut P Balaji
//

#include"UsesStatTable.h"

UsesStatTable::UsesStatTable() {
	initialised = false;
}

UsesStatTable* UsesStatTable::obj = NULL;


UsesStatTable* UsesStatTable::getInstance(){
		if(!obj) 
			obj = new UsesStatTable;
		return obj;
}

BOOLEAN UsesStatTable::addEntry(VAR_LIST varUsed) {
	tempVarUsed.push_back(varUsed);
	return true;
}

BOOLEAN UsesStatTable::addEntry(STATEMENT_INDEX n, VAR_LIST varUsed) {
	for(int i=0;i<varUsed.size();i++) {
		bool temp = false;
		for(int j=0;j<variablesUsed.at(n-1).size();j++) {
			if(varUsed.at(i) == variablesUsed.at(n-1).at(j)) {
				temp = true;
			}
		}
		if(temp == false) {
			variablesUsed.at(n-1).push_back(varUsed.at(i));
		}
	}
	return true;
}


BOOLEAN UsesStatTable::isUsed(STATEMENT_INDEX n, VAR_INDEX ind) {

	for(int i=0;i<variablesUsed.at(n-1).size();i++) {
		if(ind == variablesUsed.at(n-1).at(i)) {
			return true;
		}
	}
	return false;
}

VAR_LIST UsesStatTable::getVarUsed(STATEMENT_INDEX n) {
	if(initialised == true) {
		return variablesUsed[n-1];
	}
	else {
		return tempVarUsed[n-1];
	}
}


void UsesStatTable::initialise() {
	initialised = true;
	for(int i =tempVarUsed.size()-1;i>=0;i--) {
		variablesUsed.push_back(tempVarUsed.at(i));
	}
}